package br.com.sysprova.view;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPasswordField;

import br.com.sysprova.controller.UsuarioController;
import br.com.sysprova.model.bean.Aluno;
import br.com.sysprova.model.bean.Professor;
import br.com.sysprova.model.bean.Usuario;

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window.Type;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class Login {

	private JFrame frmSysprova;
	private JTextField txtMatricula;
	private JPasswordField txtPasswd;
	private JButton btnLogin;
	private JLabel lblEsqueceuASenha;
	private JLabel lblCadastrese ;
	
	private UsuarioController usuarioController;
	
	/**
	 * Create the application.
	 */
	public Login() {
		usuarioController = new UsuarioController();
		initialize();
		frmSysprova.setVisible(true);
		frmSysprova.setFocusable(true);
		frmSysprova.requestFocus();
		txtMatricula.requestFocus();
	}
	
	
	public Point centralizarFrame (){  
		//Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();  
        Rectangle r      = frmSysprova.getBounds();  
      // Dimens?es da janela  
        int widthSplash = r.width ;  
        int heightSplash = r.height; 
		
		return ( new Point (((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()/2) - (widthSplash/2),  
	            			((int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()/2) - (heightSplash/2))
			   );  
	} 

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmSysprova = new JFrame();
		frmSysprova.setType(Type.UTILITY);
		frmSysprova.setResizable(false);
		frmSysprova.setSize(new Dimension(497, 331));
		frmSysprova.setTitle("SysProva");
		frmSysprova.setAlwaysOnTop(true);
		frmSysprova.setBounds(100, 100, 450, 300);
		frmSysprova.setLocation(centralizarFrame());
		frmSysprova.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmSysprova.getContentPane().setLayout(null);
		
		
		txtMatricula = new JTextField();
		txtMatricula.setBounds(166, 84, 132, 20);
		frmSysprova.getContentPane().add(txtMatricula);
		txtMatricula.setColumns(10);
		
		btnLogin = new JButton("Entrar");
		eventoBotaoLogin();
		
		btnLogin.setBounds(166, 140, 132, 23);
		frmSysprova.getContentPane().add(btnLogin);
		
		JLabel lblMatrcula = new JLabel("Matr\u00EDcula");
		lblMatrcula.setBounds(101, 87, 55, 14);
		frmSysprova.getContentPane().add(lblMatrcula);
		
		JLabel lblSenha = new JLabel("Senha");
		lblSenha.setBounds(111, 112, 36, 14);
		frmSysprova.getContentPane().add(lblSenha);
		
		txtPasswd = new JPasswordField();
		txtPasswd.setBounds(166, 109, 132, 20);
		frmSysprova.getContentPane().add(txtPasswd);
		
		JLabel lblLogin = new JLabel("LOGIN");
		lblLogin.setFont(new Font("SketchFlow Print", Font.BOLD, 11));
		lblLogin.setBounds(212, 60, 46, 14);
		frmSysprova.getContentPane().add(lblLogin);
		
		final JLabel lblSair = new JLabel(new ImageIcon("imagens//sign_out.png"));
		eventoLbSair(lblSair);
		
		lblSair.setBounds(394, 275, 46, 14);
		
		frmSysprova.getContentPane().add(lblSair);
		
		lblCadastrese = new JLabel("Cadastre-se");
		eventoLbCadastro();
		
		lblCadastrese.setBounds(166, 169, 132, 14);
		frmSysprova.getContentPane().add(lblCadastrese);
		
		lblEsqueceuASenha = new JLabel("Esqueceu a senha?");
		eventoLbEsqueceuSenha();
		
		lblEsqueceuASenha.setBounds(166, 186, 132, 14);
		frmSysprova.getContentPane().add(lblEsqueceuASenha);
	}

	private void eventoLbEsqueceuSenha() {
		lblEsqueceuASenha.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
			}
			@Override
			public void mouseEntered(MouseEvent e) {
				lblEsqueceuASenha.setCursor(new Cursor(Cursor.HAND_CURSOR));
			}
		});
		
	}

	private void eventoLbCadastro() {
		lblCadastrese.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				TelaSelecaoCadastro selecCad = new TelaSelecaoCadastro();
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				lblCadastrese.setCursor(new Cursor(Cursor.HAND_CURSOR));
			}
		});
		
	}

	private void eventoLbSair(final JLabel lblSair) {
		lblSair.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				frmSysprova.dispose();
			}
			@Override
			public void mouseEntered(MouseEvent e) {
				lblSair.setCursor(new Cursor(Cursor.HAND_CURSOR));
			}
		});
		
	}

	private void eventoBotaoLogin() {
		btnLogin.addActionListener(
				new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String login=txtMatricula.getText();
					String senha=String.valueOf(txtPasswd.getPassword());
					Object o = usuarioController.autentica(login,senha );
					if (o != null) {
						if (o instanceof Aluno) {
							System.out.println("É Aluno");
						} else if (o instanceof Professor) {
							System.out.println("É Professor");
							TelaInicial frame = new TelaInicial(new PanelProfessor(o));
						} else if (o instanceof Usuario) {
							System.out.println("É Usuario");
						}else{
							System.out.println("Não é nada");
						}
						frmSysprova.dispose();
					}
					
				}			
			});
		
	}
}
